<template>
  <div>
    <el-dialog title="上传PDI" :visible.sync="visiblePdi" width="200px">
      <el-upload
        :action="action"
        :limit="1"
        :data="{
          prefixMenu: 'image',
        }"
        :headers="headers"
        :multiple="false"
        :file-list="filelist"
        accept="image/*"
        list-type="picture-card"
        :on-preview="handlePictureCardPreview"
        :on-remove="handleRemove"
        :on-success="onSuccess"
      >
        <i class="el-icon-plus"></i>
      </el-upload>
      <el-dialog :visible.sync="dialogVisible">
        <img width="100%" :src="dialogImageUrl" alt="" />
      </el-dialog>
      <div slot="footer" class="dialog-footer">
        <el-button @click="submitPdi('cancel')">取 消</el-button>
        <el-button type="primary" @click="submitPdi('ok')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'
import {getImageUrl} from '@/utils';
export default {
  props: {
    visiblePdi: {
      type: Boolean,
      default: () => false,
    },
  },
 
  data() {
    return {
      action: `${process.env.BASE_API}/base/attachment/upload`,
      dialogImageUrl: '',
      filelist:[],
      dialogVisible: false,
      headers: {
        token: getToken(),
      },
    }
  },
  methods: {
    onSuccess(response) {
      if (response.success) {
        this.$emit('input', getImageUrl(response.data.id))
      }
    },
    submitPdi(type){
        this.$emit('pdi-submit',type)
        setTimeout(() => {
            this.filelist=[];
        }, 500);
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
  },
}
</script>

<style lang="scss" scoped>
</style>